
/*    This file is part of the managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanaintlib.machine;

import org.managarm.jmanavalib.core.VaDatatype;

public final class VmCellUInt extends VmCell {
	private final long value;
	
	public VmCellUInt(long value) {
		this.value = value;
	}
	
	@Override
	public final long valueAsUInt() {
		return this.value;
	}
	
	@Override public final VmCell castTo(VaDatatype datatype) {
		if(datatype instanceof VaDatatype.DtPrimitive) {
			final VaDatatype.DtPrimitive type
					= (VaDatatype.DtPrimitive)datatype;
			if(type.identity == VaDatatype.DtPrimitive.PRIM_CHAR) {
				return new VmCellChar(value);
			}else if(type.identity == VaDatatype.DtPrimitive.PRIM_UBYTE) {
				return new VmCellUInt(value & 0xFFL);
			}else if(type.identity == VaDatatype.DtPrimitive.PRIM_USHORT) {
				return new VmCellUInt(value & 0xFFFFL);
			}else if(type.identity == VaDatatype.DtPrimitive.PRIM_UINT) {
				if(value > (1L<<32L))
					System.out.println("FIXME: VmCellUInt.castTo()");
				// FIXME: casts to uint
				return this;
			}
		}
		
		throw new UnsupportedOperationException("Invalid cast operation for "
				+ getClass().getSimpleName());
	}
}
